package com.anuo.app.modules.system.controller;

import com.anuo.app.common.basedata.PayDic;
import com.anuo.app.common.entity.Request;
import com.anuo.app.common.globalhandle.BaseController;
import com.anuo.app.common.globalhandle.GlobalLogHandler;
import com.anuo.app.common.utils.*;
import com.anuo.app.modules.system.entity.*;
import com.anuo.app.modules.system.service.SystemService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class VipController extends BaseController {
    private static GlobalLogHandler logger = new GlobalLogHandler(VipController.class);

    @Autowired
    SystemService systemService;

    /**
     * 保存待支付订单(支付流程第1步)
     *
     * @param reqString
     * @return
     */
    @PostMapping(value = "v1/apiSaveVipBillWaitPay", produces = {"application/json;charset=UTF-8"})
    public String apiSaveVipBillWaitPay(@RequestBody String reqString) {
        VipBill vipBill = JsonUtil.fromJson(reqString, new TypeReference<Request<VipBill>>() {
        }).getFirstEntity();
        Assert.isZero(vipBill.getTotal_amount(), "金额不能为0");
        vipBill.setPay_state(PayDic.WAIT_PAY);
        String result = systemService.saveVipBill(vipBill);
        return rspSuccess(result);
    }

    /**
     * 获取订单(支付流程第2步)
     *
     * @param reqString
     * @return
     */
    @PostMapping(value = "v1/apiGetVipBill", produces = {"application/json;charset=UTF-8"})
    public String apiGetVipBill(@RequestBody String reqString) {
        SystemFilter systemFilter = JsonUtil.fromJson(reqString, new TypeReference<Request<SystemFilter>>() {
        }).getFirstEntity();
        Assert.isEmpty(systemFilter.getOrderno(), "订单号不能为空");
        VipBill vipBill = systemService.getVipBill(systemFilter);
        return rspSuccess(vipBill);
    }

    /**
     * 获取支付字符串(支付流程第3步)
     *
     * @param reqString
     * @param request
     * @return
     */
    @PostMapping(value = "v1/apiGetPayString", produces = {"application/json;charset=UTF-8"})
    public String apiGetPayString(@RequestBody String reqString, HttpServletRequest request) {
        SystemFilter systemFilter = JsonUtil.fromJson(reqString, new TypeReference<Request<SystemFilter>>() {
        }).getFirstEntity();
        Assert.isEmpty(systemFilter.getOrderno(), "订单号不能为空");
        //获取客服端Ip (如果不是有效的就先随便给个有效的, 这样才能成功调用微信的api)
        String clientIp = "";
        if (!NetUtil.validIP(request.getRemoteAddr())) {
            clientIp = "192.168.1.195";
        }
        //获取支付字符串
        String result = systemService.getPayString(systemFilter, clientIp);
        return rspSuccess(result);
    }

    /**
     * 保存已支付订单(支付流程第4步)
     *
     * @param reqString
     * @return
     */
    @PostMapping(value = "v1/apiSaveVipPaid", produces = {"application/json;charset=UTF-8"})
    public String apiSaveVipPaid(@RequestBody String reqString) {
        VipBill vipBill = JsonUtil.fromJson(reqString, new TypeReference<Request<VipBill>>() {
        }).getFirstEntity();
        Assert.isEmpty(vipBill.getOrderno(), "订单号不能为空");
        systemService.saveVipPaid(vipBill);
        return rspSuccess();
    }


}